package absc.wizard;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import absc.bd.ConexaoBanco;

public class ABSCWizardMySqlPage extends WizardPage implements KeyListener, SelectionListener{

	private Text txtHostname, txtPort, txtUsername, txtPswd;
	private Button btTestConnection;
	
	protected ABSCWizardMySqlPage() {
		super("ABSC Project");
		setTitle("New ABSC Project");
		setDescription("My Sql information");		
	}

	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub		
	}

	public void keyReleased(KeyEvent keyEvent) {
		String errorMessage;
		
		errorMessage = null;
		
		if (keyEvent.getSource().equals(this.txtHostname)){
			if (this.txtHostname.getText().equalsIgnoreCase("")){
				errorMessage = "A MySql hostname is empty";				
			}
			else{
				errorMessage = null;
			}
		}
		
		if (keyEvent.getSource().equals(this.txtPort)){
			if (this.txtPort.getText().equalsIgnoreCase("")){
				errorMessage = "A MySql port is empty";				
			}
			else{
				errorMessage = null;
			}
		}		
		
		if (keyEvent.getSource().equals(this.txtUsername)){
			if (this.txtUsername.getText().equalsIgnoreCase("")){
				errorMessage = "A MySql username is empty";
			}
			else{
				errorMessage = null;
			}
		}
		
		if (keyEvent.getSource().equals(this.txtPswd)){
			if (this.txtPswd.getText().equalsIgnoreCase("")){
				errorMessage = "A MySql password is empty";
			}
			else{
				errorMessage = null;
			}
		}
		
		setPageComplete(false);
		setErrorMessage(errorMessage);
	}

	public void createControl(Composite parent) {
		GridLayout layoutTela;		
		Composite tela;
		GridData layoutLabel, layoutEdit;
		CLabel lblHostname, lblPort, lblUsername, lblPswd;
		
		initializeDialogUnits(parent);
		
		layoutTela = new GridLayout();
		layoutTela.numColumns = 2;
		
		tela = new Composite(parent, SWT.NONE);
		tela.setLayout(layoutTela);
		
		layoutLabel = new GridData();
		layoutLabel.horizontalAlignment = SWT.RIGHT;
		layoutLabel.widthHint = convertWidthInCharsToPixels(20);
		
		layoutEdit = new GridData();
		layoutEdit.heightHint = -1;
		layoutEdit.widthHint = convertWidthInCharsToPixels(50);
		
		lblHostname = new CLabel(tela, SWT.NONE);		
		lblHostname.setText("Hostname:");
		lblHostname.setLayoutData(layoutLabel);
		
		txtHostname = new Text(tela, SWT.BORDER);
		txtHostname.setOrientation(33554432);
		txtHostname.setLayoutData(layoutEdit);
		txtHostname.addKeyListener(this);		
		txtHostname.setText("localhost");
		
		lblPort = new CLabel(tela, SWT.NONE);		
		lblPort.setText("Port:");
		lblPort.setLayoutData(layoutLabel);
		
		txtPort = new Text(tela, SWT.BORDER);
		txtPort.setOrientation(33554432);
		txtPort.setLayoutData(layoutEdit);
		txtPort.addKeyListener(this);
		txtPort.setText("3306");		

		lblUsername = new CLabel(tela, SWT.NONE);		
		lblUsername.setText("Username:");
		lblUsername.setLayoutData(layoutLabel);
		
		txtUsername = new Text(tela, SWT.BORDER);
		txtUsername.setOrientation(33554432);
		txtUsername.setLayoutData(layoutEdit);
		txtUsername.addKeyListener(this);
		txtUsername.setText("root");

		lblPswd = new CLabel(tela, SWT.NONE);		
		lblPswd.setText("Password:");
		lblPswd.setLayoutData(layoutLabel);
		
		txtPswd = new Text(tela, SWT.PASSWORD | SWT.BORDER);
		txtPswd.setOrientation(33554432);
		txtPswd.setLayoutData(layoutEdit);
		txtPswd.addKeyListener(this);
		
		btTestConnection = new Button(tela, SWT.NONE);
		btTestConnection.setText("Test Connection");
		btTestConnection.addSelectionListener(this);

		setControl(tela);
		Dialog.applyDialogFont(tela);
		setPageComplete(false);
	}

	public ConexaoBanco getConexaoBanco() {
		return new ConexaoBanco(this.txtHostname.getText(), this.txtPort.getText(), this.txtUsername.getText(), 
				this.txtPswd.getText());
	}

	public void widgetDefaultSelected(SelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void widgetSelected(SelectionEvent event) {
		if (event.getSource().equals(this.btTestConnection)){
			ConexaoBanco bd = new ConexaoBanco(this.txtHostname.getText(), this.txtPort.getText(), this.txtUsername.getText(), 
					this.txtPswd.getText());
			
			if (bd.conectaBanco()){
				MessageDialog.openInformation(getShell(), "Test MySql Connection", "Ping suceeded");
				setPageComplete(true);
				setErrorMessage(null);
			}
			else{
				MessageDialog.openInformation(getShell(), "Test MySql Connection", "Ping failed");
				setPageComplete(false);
				setErrorMessage("Ping failed");				
			}
		}
	}
}